package com.icw.configuration.web.interceptor.outest;

import java.util.Map;

import org.aopalliance.intercept.MethodInvocation;
import org.springframework.aop.Advisor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import com.icw.configuration.ConstSettings;
import com.icw.decoupling.defi.tool.DataExchange;
import com.icw.utility.BeanCtxUtils;
import com.icw.utility.IcwUtils;

@Configuration
public class ArgumentsInterceptor {

	@Bean
	public Advisor transformInputParamsAndCreateOutputContainer() throws Exception {
		return IcwUtils.cutExpression(this::parseInputParams, ConstSettings.OUTEST_CUT_POINT, ConstSettings.FIRST_ORDER + 2);
	}

	private void parseInputParams(MethodInvocation invocation) throws Exception {
		Integer params_index = null;
		for (int i = 0; i < invocation.getArguments().length - 1; i++) {
			if (Map.class.isInstance(invocation.getArguments()[i])) {
				params_index = i;
				break;
			}
		}
		if (params_index != null) {
			invocation.getArguments()[params_index] = de().parseMap(invocation.getArguments()[params_index]);
		}
	}

	private static DataExchange de() {
		return BeanCtxUtils.getBeanByType(DataExchange.class);
	}
}
